#!/bin/sh

fsim_base_module="@CMAKE_CURRENT_BINARY_DIR@/../kedr_fsim_base.ko"

module_a_name="fsim_base_test_a"
module_a="@CMAKE_CURRENT_BINARY_DIR@/module_a/${module_a_name}.ko"
module_b="@CMAKE_CURRENT_BINARY_DIR@/module_b/fsim_base_test_b.ko"
module_c="@CMAKE_CURRENT_BINARY_DIR@/module_c/fsim_base_test_c.ko"
current_value_file="/sys/module/${module_a_name}/parameters/current_value"
external_value_file="/sys/module/${module_a_name}/parameters/a_external_value"

device=kedr_test_device
commands_file="@CMAKE_CURRENT_BINARY_DIR@/commands"
do_commands_script="sh @CMAKE_BINARY_DIR@/tests/scripts/do_commands.sh"

cat > "$commands_file" << eof

on_load insmod "$fsim_base_module" || ! printf "Cannot load fault simulation base module into kernel.\n"
on_unload rmmod "$fsim_base_module" || ! printf "Cannot unload fault simulation base module.\n"
on_load insmod "$module_a" || ! printf "Cannot load module 'a' into kernel.\n"
on_unload rmmod "$module_a" || ! printf "Failed to unload module 'a'.\n"

eof

if ! $do_commands_script "$commands_file" load; then
    printf "Cannot initialize test.\n"
    exit 1
fi

if ! insmod "$module_b"; then
    printf "Cannot load module 'b' into kernel.\n"
    $do_commands_script "$commands_file" unload
    exit 1
fi

if ! insmod "$module_c"; then
    printf "Cannot load module 'c' into kernel.\n"
    rmmod "$module_b"
    $do_commands_script "$commands_file" unload
    exit 1
fi

external_value=`cat "$external_value_file"`
if test "$external_value" != "5"; then
    printf "'external_value' for module 'a' was expected to become '5' (result of destroy function of 'b') but it is '%s'.\n" "$external_value"
    rmmod "$module_c"
    rmmod "$module_b"
    $do_commands_script "$commands_file" unload
    exit 1
fi

dd "if=/dev/$device" of=/dev/null bs=1 count=1
current_value=`cat "$current_value_file"`

if test "$current_value" != "29"; then
    printf "The simulation of read point was expected to return '29', but it returned '%s'.\n" "$current_value"
    rmmod "$module_c"
    rmmod "$module_b"
    $do_commands_script "$commands_file" unload
    exit 1
fi

if ! rmmod "$module_b"; then
    printf "Error occured while unloading module 'b'.\n"
    rmmod "$module_c"
    $do_commands_script "$commands_file" unload
    exit 1
fi

dd "if=/dev/$device" of=/dev/null bs=1 count=1
current_value=`cat "$current_value_file"`

if test "$current_value" != "29"; then
    printf "The simulation of read point was expected to return '29' but it returned '%s'.\n" "$current_value"
    rmmod "$module_c"
    rmmod "$module_b"
    $do_commands_script "$commands_file" unload
    exit 1
fi

if ! rmmod "$module_c"; then
    printf "Error occured while unloading module 'c'.\n"
    $do_commands_script "$commands_file" unload
    exit 1
fi

dd "if=/dev/$device" of=/dev/null bs=1 count=1
current_value=`cat "$current_value_file"`

if test "$current_value" != "0"; then
    printf "The simulation of read point was expected to return '0' (module 'c' is unloaded) but it returned '%s'.\n" "$current_value"
    $do_commands_script "$commands_file" unload
    exit 1
fi

external_value=`cat "$external_value_file"`
if test "$external_value" != "35"; then
    printf "'external_value' for module 'a' was expected to become '35' (result of destroy function of 'c') but it is '%s'.\n" "$external_value"
    $do_commands_script "$commands_file" unload
    exit 1
fi

if ! $do_commands_script "$commands_file" unload; then
    printf "Errors occured while finalizing the test.\n"
    exit 1
fi
